local filename = "getactivityaward.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local filelog = require "filelog"
local redisdao = require "redisdao"
local base = require "base"
require "pubenum"

local json = require "cjson"
json.encode_sparse_array(true,1,1)

local service = nil
local CMD = {
	service = nil
}

--奖励id
local PRIZE_ID = {
    FANGKA = 1,     --房卡
    DIAMOND = 2,    --金豆
    COIN = 3,       --金币
}

--奖励名
local PRIZE_NAME = {
    [PRIZE_ID.FANGKA] = "fangka",     --房卡
    [PRIZE_ID.DIAMOND] = "diamond",   --金豆
    [PRIZE_ID.COIN] = "coin",         --金币
}

--是否正在处理中
local is_handling = false

--获取活动奖励
local function getactivityaward(rid, msgbody)
    local response = { }
    local actrecord = nil
    local prize_id = msgbody.prize_id or 0

    --设置领取记录key
    local record_key = ":"
    if msgbody.activity_id == EACTIVITY_IDS.REDPACKET_WATER then    --流水红包
        record_key = record_key .. "rpwaterrecord"
    else
    
    end
    
    --判断是否已领取过
    local result, data = redisdao.query_data(".gmmqredis", "get", rid .. record_key)
    if result and data then
        actrecord = json.decode(data)
        if actrecord and (actrecord[prize_id] 
        or actrecord[tostring(prize_id)]) then
            return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "不能重复领取奖励"} 
        end
    end

    --获得活动奖励列表
    local result_data = service:getactivityinfo(rid, msgbody)
    if result_data.retcode then
        result_data.retstr = "领取奖励失败"
        return result_data
    end
    
    local activity_prize_list = json.decode(result_data.activity_prize_list)
    if not activity_prize_list or not activity_prize_list[prize_id] then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "没有可领取的奖励"} 
    end
    
    local changeitems = { }
    local itemvalues = { }
    local prize_item = activity_prize_list[prize_id]
    
    --处理流水红包奖励，活动id为1001
    if result_data.activity_id == EACTIVITY_IDS.REDPACKET_WATER then
        local curwater = service.money.today_water or 0
        if curwater < tonumber(prize_item.water) then
            return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "流水未达到要求，不能领取奖励"} 
        end
        
        --随机红包金额
        local value = 0
        local minval = tonumber(prize_item.minval) or 0
        local midval = ptonumber(rize_item.midval) or 0
        local maxval = tonumber(prize_item.maxval) or 0
        local r = base.get_random(1,2)
        if r == 1 then
            value = base.get_random(minval, midval)
        else
            value = base.get_random(minval, maxval)
        end
        
        --修改数据
        local prizename = PRIZE_NAME[prize_item.id]
        local begin = service.money[prizename] or 0
        itemvalues[prizename] = { }
        itemvalues[prizename].begin = begin
        itemvalues[prizename].value = value
        changeitems[prizename] = (changeitems[prizename] or 0) + value
    else
        return {retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "奖励不存在"}
    end
    
    --没有奖励，直接返回
    if next(changeitems) == nil then
        return {retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "奖励不存在"}
    end
        
    local setargs = service:change_money(changeitems)
	if setargs then
        --记录日志
        for key,item in pairs(itemvalues) do
            local logstr = nil
            local comment = { }
            local now = timetool.get_time()
            comment.tradeid = skynet.call(".nodeuuid", "lua", "getuuid")
            comment.table_uuid = "actynode" .. "_" .. 0 .. "_" .. 0 .. "_" .. now
            if key == "diamond" then
                logstr = string.format("%d,%d,%d,%d,%d,%d,%d,%d,%d,%s,%s,%s", EDiamondChange.REDPACKETWATER, rid, service.info.level, item.value, 0, 0, item.begin, service.money.diamond, service.money.lock_diamond, comment.table_uuid, now, comment.tradeid)
                skynet.send(".diamondlog", "lua", "wrie", logstr)
            elseif key == "coin" then
                logstr = string.format("%d,%d,%d,%d,%d,%d,%d,%d,%d,%s,%s,%s", ECoinChange.REDPACKETWATER, rid, service.info.level, item.value, 0, 0, item.begin, service.money.coin, service.money.lock_coin, comment.table_uuid, now, comment.tradeid)
                skynet.send(".coinlog", "lua", "write", logstr)
            end
        end
        
        --保存领取记录
        actrecord = actrecord or {}
        actrecord[prize_id] = prize_id
        redisdao.query_data(".gmmqredis", "set", rid .. record_key, json.encode(actrecord))
    else
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "获取奖励失败"}
    end
    response.activity_id = msgbody.activity_id
    response.prize_id = msgbody.prize_id
    
    return response
end

--
--获取活动奖励
--
function CMD.process(rid, msgbody)
    if not service then
        service = CMD.service
    end
    msgbody = msgbody or {}

    --判断是否正在处理中
    if is_handling then
        return {}
    else
        is_handling = true
    end
    
    --处理
    local response = getactivityaward(rid, msgbody)
    is_handling = false

    return response
end

return CMD
